package platform.config;

import com.alibaba.fastjson.JSONObject;
import platform.resource.SensorMgr;

import java.util.HashMap;
import java.util.Map;

public class CtxServerConfig {
    private volatile static CtxServerConfig INSTANCE = null;
    private final boolean serverOn;
    private final String ctxValidator;
    private final String ctxChecker;
    private final String ctxScheduler;
    private final String baseRuleFile;
    private final String baseBfuncFile;
    private final String basePatternFile;
    private final String baseMfuncFile;
    private final Map<String, SensorMgr> sensorMgrMap;


    public static CtxServerConfig getInstance(){
        return INSTANCE;
    }
    public static CtxServerConfig getInstance(JSONObject object){
        INSTANCE = new CtxServerConfig(object);
        return INSTANCE;
    }


    private CtxServerConfig(JSONObject object){
        serverOn = object.getBoolean("serverOn");
        ctxValidator = object.getString("CtxValidator");
        ctxChecker = ctxValidator.split("\\+")[0];
        ctxScheduler = ctxValidator.split("\\+")[1];
        baseRuleFile = object.getString("baseRuleFile");
        baseBfuncFile = object.getString("baseBfuncFile");
        basePatternFile = object.getString("basePatternFile");
        baseMfuncFile = object.getString("baseMfuncFile");
        sensorMgrMap = new HashMap<>();
    }


    public void addSensorConfig(SensorMgr sensorMgr){
        this.sensorMgrMap.put(sensorMgr.getSensorName(), sensorMgr);
    }


    public boolean isServerOn() {
        return serverOn;
    }

    public String getCtxValidator() {
        return ctxValidator;
    }

    public String getCtxChecker() {
        return ctxChecker;
    }

    public String getCtxScheduler() {
        return ctxScheduler;
    }

    public String getBaseRuleFile() {
        return baseRuleFile;
    }

    public String getBaseBfuncFile() {
        return baseBfuncFile;
    }

    public String getBasePatternFile() {
        return basePatternFile;
    }

    public String getBaseMfuncFile() {
        return baseMfuncFile;
    }

    public Map<String, SensorMgr> getSensorConfigMap() {
        return sensorMgrMap;
    }

    @Override
    public String toString() {
        return "CtxServerConfig{" +
                "serverOn=" + serverOn +
                ", ctxValidator='" + ctxValidator + '\'' +
                ", ctxChecker='" + ctxChecker + '\'' +
                ", ctxScheduler='" + ctxScheduler + '\'' +
                ", baseRuleFile='" + baseRuleFile + '\'' +
                ", baseBfuncFile='" + baseBfuncFile + '\'' +
                ", basePatternFile='" + basePatternFile + '\'' +
                ", baseMfuncFile='" + baseMfuncFile + '\'' +
                ", sensorMgrMap=" + sensorMgrMap +
                '}';
    }
}
